#!/usr/bin/env bash
# msg_type =[ docker_img_build ,  push_to_repo , deploy ] 
msg_type=$1
slu_name=$2
name=$3
wechat_hook_key=$4
if [[ ${msg_type} == "docker_img_build" ]];
then
  img_build_state=$5
  user_name=$6
  commit_branch=$7
  commit_sha=$8
  commit_desc=$9
  commit_message=$10

  msg_content="\"${user_name} 对 ${commit_branch} 进行了提交 . \n  commit sha:${commit_sha}.\n  commit_desc:${commit_desc}.\n  commit_message:${commit_message}\n  docker镜像 打包结果: ${img_build_state}！\n  [点击查看](https://git.isecsp.com/xicheng/${name}/pipelines)\""
elif [[  ${msg_type} == "push_to_repo" ]]
then
  repo_name=$5
  img_name=$6
  push_result=$7

  msg_content="\"${img_name} \n推送到: ${repo_name}  \n结果: ${push_result} \n[点击查看](https://git.isecsp.com/xicheng/${name}/)\""

elif [[ ${msg_type} == "deploy" ]]
then
  deploy_server=$5
  docker_repo=$6
  slu_name=$7
  app_name=$8
  commit_branch=$9
  commit_sha=$10
  deploy_state=$11

  msg_content="\"${docker_repo}/${slu_name}/${app_name}-${commit_branch}:${commit_sha} \n在 ${deploy_server} 上部署 【${deploy_state}】.\n container_name: ${slu_name}-${app_name} \n[点击查看](https://git.isecsp.com/xicheng/${name}/pipelines)\""
fi

echo "
{
  \"msgtype\": \"markdown\",
  \"markdown\": {
    \"content\": ${msg_content}
  }
}"
#https://httpbin.isecsp.com/post
curl "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=${wechat_hook_key}" \
-H 'Content-Type: application/json' \
-d "
{
  \"msgtype\": \"markdown\",
  \"markdown\": {
    \"content\": ${msg_content}
  }
}"
